PRO Frame_Subset_via_Shapefile_DEFINE_BUTTONS, buttonInfo
  ;
  COMPILE_OPT idl2
  ENVI_DEFINE_MENU_BUTTON, buttonInfo,      $
    value = 'Frame Subset via Shapefile',   $
    uvalue = 'option 2',                    $
    event_pro = 'Frame_Subset_via_Shapefile',   $
    ref_value = 'Subset Data via ROIs'

END


; Add the extension to the toolbox. Called automatically on ENVI startup.
PRO Frame_Subset_via_Shapefile_extensions_init

  ; Set compile options
  COMPILE_OPT IDL2

  ; Get ENVI session
  e = ENVI(/CURRENT)

  ; Add the extension to a subfolder
  e.AddExtension, 'Frame Subset via Shapefile', $
    'Frame_Subset_via_Shapefile', PATH=''
END

function NDWI

end

function MNDWI

end

function NWI

end

function ISPM

end

pro water_index
  ; Set compile options
  COMPILE_OPT IDL2

  ; General error handler
  CATCH, err
  IF (err NE 0) THEN BEGIN
    CATCH, /CANCEL
    !NULL = DIALOG_MESSAGE('ERROR: '  $
      + !ERROR_STATE.MSG,             $
      /ERROR, TITLE='ENVI Error')
    MESSAGE, /RESET
    RETURN
  ENDIF

  ;******************************************
  ; Insert your ENVI Extension code here...
  ;******************************************

  ;打开栅格数据
  ENVI_SELECT, fid=fid, /NO_DIMS, $
    /FILE_ONLY, POS=POS,          $
    TITLE='Select input file'
  IF fid[0] EQ -1 THEN RETURN

  ;  envi, /restore_base_save_files
  ;  filename=envi_pickfile(title='请选择处理图像')
  ;  if (filename eq '') then return
  ;  envi_open_file, filename, r_fid=fid
  ;  if (fid eq -1) then begin
  ;    envi_batch_exit
  ;    return
  ;  endif
  envi_file_query, fid, ns=ns, nl=nl, nb=nb
  dims = [-1l, 0, ns-1, 0, nl-1]
  pos  = lindgen(nb)
  ENVI_DOIT,'envi_stats_doit', fid=fid, pos=pos, $
    dims=dims, comp_flag=4,cov=cov, dmin=dmin, dmax=dmax, $
    mean=mean, stdv=stdv, hist=hist,PLOT_FLAG=16,R_FID=R_FID
  ENVI_FILE_QUERY, R_FID, ns=ns, nl=nl
  dims = [-1, 0, ns-1, 0, nl-1]
  data=ENVI_GET_DATA(fid=R_FID, dims=dims, pos=1)
  envi_report_init,['Input File:'],$
    title='波段选择处理进程-计算波段最佳指数',base=base
  envi_report_inc,base,nb
  OIF=dblARR(nb*nb*nb)
  for I=0,nb-1 do begin
    envi_report_stat,base,i,nb
    For J=I+1,nb-1 do begin
      for K=J+1,nb-1 do begin
        OIF[K+J*nb+I*nb*nb]=(stdv[I]+stdv[J]+stdv[K])/(data[I,J]+data[I,K]+data[J,K])
      endfor
    endfor
  endfor
  envi_report_init,base=base,/finish
  oifsort=sort(oif)
  print,'     选用OIF计算方法得出的结果'
  envi_report_init,['Input File:'],$
    title='波段选择处理进程-计算波段序号1',base=base
  envi_report_inc,base,20
  zuhe_num=factorial(nb)/(factorial(nb-3)*factorial(3))
  data = FLTARR(4, zuhe_num,/NOZERO)
  listvalue='  Zuhe         OIF'+'n'
  for i=0,zuhe_num-1 do begin
    envi_report_stat,base,i,20
    whereMax=where(OIF EQ oif[oifsort[nb*nb*nb-1-i]])
    b1=whereMax[0] / (nb*nb)+1
    bands=whereMax[0] MOD (nb*nb)
    b2=bands / nb+1
    b3=bands MOD nb+1
    data[0, i]=b1
    data[1, i]=b2
    data[2, i]=b3
    data[3, i]=oif[oifsort[nb*nb*nb-1-i]]
    ;data[4,i]=B3*100+b2*10+b1
    listvalue +=strcompress(b1) + strcompress(b2)+strcompress(b3)+'    '+strcompress(oif[oifsort[nb*nb*nb-1-i]])+'n'
    print,b1,b2,b3,oif[oifsort[nb*nb*nb-1-i]], /STDIO_NON_FINITE
  endfor
  envi_report_init,base=base,/finish

  ;创建tlb
  tlb = WIDGET_BASE($
    xsize = 370,YSIZE=450, $
    /column, $
    title ='Zuhe_OIF')
  WIDGET_CONTROL,tlb,/realize
  ;使用list组件
  listvalue1 = strsplit(listvalue,'n',/extract)
  ;  wlist = WIDGET_LIST(tlb,value = listvalue1,$
  ;    /MULTIPLE, $
  ;    SCR_YSIZE =250)
  listvalue=strcompress(data)
  table = WIDGET_table(tlb,COLUMN_LABELS = 'B'+STRTRIM(INDGEN(4),2),$
    /EDITABLE, DISJOINT_SELECTION=1, $
    xsize = 4, ysize = zuhe_num,uName  = 'OIF')
  WIDGET_CONTROL, table, set_value = data
  ;WIDGET_CONTROL, tlb,set_uvalue= PTR_NEW({listvalue:listvalue})
  ;事件响应函数
  XMANAGER,'WIDGET_LIST_EXAMPLE',tlb,/no_block
  ;b1 = BARPLOT(data[3,*], data[4, *], INDEX=0, NBARS=3, $
  ;    FILL_COLOR='green', YRANGE=[0, 1000], YMINOR=0, $
  ;    YTITLE='Number Breeding Individuals', XTITLE='Year', $
  ;    TITLE = 'North American Breeding Bird Survey - ' + $
  ;    'Counts for Maine', $
  ;    name = 'Pine Warbler')
end


